/**
 *	TrueMusic - simple Java music player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *
 *	Copyright (c)2005-2007 Ryan Macnak <rmacnak@gmail.com>
 */

package com.servequake.truemusic;

import javax.sound.sampled.*;
import java.io.*;

/**
 * Audio implemented with basic support from javax.sound.sampled.AudioSystem
 * plays AIF/WAV/AU/SND (the simple, uncompressed formats :D)
 */
public class JavaSoundAudio /*extends Audio*/ implements Runnable
{
	static public void main(String[] args) throws Exception
	{
		JavaSoundAudio x = new JavaSoundAudio();
		x.load(args[0], null);
		x.play();
	}


	Thread runner;

	String res;
	PlaybackListener l;

	int frames = 0;
	int pos = 0; //next frame to play
	int current = 0; //next frame in decoder
	boolean pause = true;
		
	public void load(String res, PlaybackListener l) throws Exception
	{
		this.res = res;
		this.l = l;
	}

	public void play()
	{
		if(pause && runner!=null) //paused
		{
			pause = false;
			notify();
			l.playbackStarted();
		}
		else if(pause && runner==null) //stopped
		{
			pause = false;
			
			// try 
			// {


			// }   
			// catch(IOException e) 
	//		{
		//		e.printStackTrace();
	//		}

			(runner = new Thread(this)).start();
			l.playbackStarted();
		}
		else if(!pause )//playing
		{
			pos = 0;
		}
	}

	public void pause()
	{
		if(pause && runner!=null) //paused
		{
			pause = false;
			notify();
			l.playbackStarted();
		}
		else if(pause && runner==null) //stopped
		{
		}
		else if(!pause )//playing
		{
			pause = true;
			l.playbackStopped();
		}
	}

	public void stop()
	{
		if(pause && runner!=null) //paused
		{
			runner = null;
			notify();
		}
		else if(pause && runner==null) //stopped
		{
		}
		else if(!pause )//playing
		{
			pause = true;
			runner = null;
		}
	}


	byte[] buffer = new byte[1024];

	public void run()
	{
/*		try
		{
			while(true)
			{
				synchronized(this)
				{
					if(runner==null) break;
					while(pause) wait();

					if(pos<current) //reset
					{
					}
					
					while(pos>current)
					{
						//skip frame
					}

					if(vol!=null)vol.setValue(l.getVolume());

		
					int 

					line.write( frame);
					pos++;
					l.frameCompleted(pos);
				}

			}
		}
		catch(Exception e)
		{
		}
		finally
		{
			pos = 0;
			pause = true;
			runner = null;
			l.frameCompleted(0);
		}
	}

*/

try{
		AudioInputStream ais = AudioSystem.getAudioInputStream(new File(res));
		ais = AudioSystem.getAudioInputStream( AudioFormat.Encoding.PCM_SIGNED, ais);

		AudioFormat format = ais.getFormat();
		DataLine.Info info = new DataLine.Info( SourceDataLine.class, format);

		SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
		line.open(format);
		line.start();


		int read = 0;

		while( (read = ais.read(buffer,0,1024)) != -1)
		{
			line.write(buffer,0,read);
		}
}catch(Exception e){e.printStackTrace();}

	}




	public int getFrames()
	{
		return 0;
	}

	public void seek(int frame) {} //ignore

	

	public String getResource(){return res;}
	public String getTitle(){return new File(res).getName();}
	public String getArtist(){return "";}
	public String getAlbum(){return "";}
	public String getTrack(){return "";}
	public String getLyrics(){return "";}
	public String getGenre(){return "";}
	public String toString(){return new File(res).getName();}

	public String formatName(){return "Aiff/Au";}
	public String[] getExtensions() {return new String[]{".au",".aif",".aiff"};}
}
